package com.css.fxfzmh.modules.templateManage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzmh.base.response.RestResponse;
import com.css.fxfzmh.logInfo.SysLog;
import com.css.fxfzmh.modules.symbolManage.entity.Symbol;
import com.css.fxfzmh.modules.templateManage.entity.Template;
import com.css.fxfzmh.modules.templateManage.param.TemplateParam;
import com.css.fxfzmh.modules.templateManage.service.TemplateService;
import com.css.fxfzmh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/10
 * @Description: 图件模板controller
 */
@RestController
@Slf4j
@RequestMapping("/template")
public class TemplateController {

    @Resource
    TemplateService templateService;

    /**
     * 分页列表
     * @param param 参数
     * @return page
     */
    @SysLog("分页查询")
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, TemplateParam param) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = templateService.getPage(curPage, pageSize,param);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存
     * @param template
     * @return
     */
    @SysLog("保存")
    @PostMapping("/save")
    public RestResponse save(@RequestBody Template template){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            templateService.save(template);
            jsonObject.put("message","保存成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @SysLog("批量删除")
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids){
        RestResponse response = null;
        try{
            templateService.delete(ids);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
